#include <interface\gl.h>

#include <cstdio>

#include "config.h"
#include "mainwin.h"
#include "gameEngine.h"

window* win = NULL;

static LRESULT CALLBACK mainProc(HWND,UINT,WPARAM,LPARAM);

mainWin::mainWin(): window(),
                    hexCol( col_md_green ),
                    blocCol( col_dk_green ),
                    occCol( col_li_red ),
                    pnjCol( col_md_red ),
                    NpnjCol( col_md_grey ),
                    d( NULL )
{
    callback = (WNDPROC)mainProc;
    create();
    ft = new font();
    ft->create();
}
mainWin::~mainWin(){
    delete ft;
}

bool mainWin::initGL(){
    window::initGL();

    glDisable(GL_ALPHA_TEST);
    glEnable(GL_DEPTH_TEST);
    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

    return true;
}

void        mainWin::drawGLScene(){
	window::drawGLScene();

    unsigned char parity;

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glDisable(GL_DEPTH_TEST);

	glLoadIdentity();

    glTranslatef(5,10,0);

    cell_t mo;

    for( int i = 0; i < GE->getMap()->getWidth(); i++ ){
        if( i % 2 ) parity = 0;
        else parity = 1;
        for( int j = 0; j < GE->getMap()->getHeight() - parity; j++ ){
            glPushMatrix();
                glScalef(SCALE,SCALE,0);
                glTranslatef(1.5*i,0.87*getGridY(j,i),0);
                if( (mo = *(GE->getMap()->getCell(i,j))) != CELL_NORMAL ){
                    glBegin(GL_POLYGON);
                    if( getStatus(CELL_OBSTACLE,mo) ){
                        glColor(&blocCol);
                    }else if( getStatus(CELL_OCCUPIED,mo) ){
                        glColor(&occCol);
                    }
                }else{
                    glBegin(GL_LINE_LOOP);
                    glColor(&hexCol);
                }
                    glVertex2f(1,0);
                    glVertex2f(0.5,0.87);
                    glVertex2f(-0.5,0.87);
                    glVertex2f(-1,0);
                    glVertex2f(-0.5,-0.87);
                    glVertex2f(0.5,-0.87);
                glEnd();
            glPopMatrix();
        }
    }

    mapObject* mo2;

    glColor(&pnjCol);

    GE->getMap()->resetMP();
    if( (mo2 = GE->getMap()->curMP()) ){
        do{
            glColor(&pnjCol);
            glPushMatrix();
                if( mo2->getX() % 2 ) parity = 0;
                else parity = 1;
                glScalef(SCALE,SCALE,0);
                glTranslatef(1.5*mo2->getX(),0.87*getGridY(mo2->getY(),mo2->getX()),0);
                glBegin( GL_POLYGON );
                    glVertex2f(-0.4, -0.4);
                    glVertex2f(0.4, -0.4);
                    glVertex2f(0.4, 0.4);
                    glVertex2f(-0.4, 0.4);
                glEnd();
            glPopMatrix();
        }while( (mo2 = GE->getMap()->nextMP()) );
    }

    GE->getMap()->neutralMutex.lock();

        GE->getMap()->resetNP();
        if( (mo2 = GE->getMap()->curNP()) ){
            do{
                glColor(&NpnjCol);
                f_coord_t curcoord = mo2->getFCoord();
                glPushMatrix();
                    glScalef(SCALE,SCALE,0);
                    glTranslatef(1.5*curcoord.X_coord,0.87*curcoord.Y_coord,0);
                    glBegin( GL_POLYGON );
                        glVertex2f(-0.4, -0.4);
                        glVertex2f(0.4, -0.4);
                        glVertex2f(0.4, 0.4);
                        glVertex2f(-0.4, 0.4);
                    glEnd();
                glPopMatrix();
            }while( (mo2 = GE->getMap()->nextNP()) );
        }

        char txt[200];
        sprintf(txt,"Rogers: %d",GE->getMap()->countNeutral());
        setName(txt);

    GE->getMap()->neutralMutex.unlock();

    GE->getMap()->neutralMutex.lock();
        GE->getMap()->dialogMutex.lock();
            dialog* dial;
            GE->getMap()->resetDP();
            if( (dial = GE->getMap()->curDP()) ){
                do{
                    if( !dial->getFont() ) dial->setFont(ft);
                    dial->display();
                }while( (dial = GE->getMap()->nextDP()) );
            }
        GE->getMap()->dialogMutex.unlock();
    GE->getMap()->neutralMutex.unlock();

    glEnable(GL_DEPTH_TEST);

    elDraw();

}

static LRESULT CALLBACK mainProc(	HWND	hWnd,
				UINT	uMsg,
				WPARAM	wParam,
				LPARAM	lParam)
{
    if( !win ) return DefWindowProc(hWnd,uMsg,wParam,lParam);
	switch (uMsg)
	{
		case WM_ACTIVATE:
		{
			if (!HIWORD(wParam))
			{
				win->setActive(true);
			}
			else
			{
				win->setActive(false);
			}

			return 0;
		}
		case WM_SYSCOMMAND:
		{
			switch (wParam)
			{
				case SC_SCREENSAVE:
				case SC_MONITORPOWER:
				return 0;
			}
			break;
		}
		case WM_CLOSE:
		{
			PostQuitMessage(0);
			return 0;
		}
		case WM_SIZE:
		{
			win->resizeGLScene(LOWORD(lParam),HIWORD(lParam));
			return 0;
		}
	}

	if( !win->getInput()->translateInputMsg(uMsg,wParam,lParam) ) return 0;

	return DefWindowProc(hWnd,uMsg,wParam,lParam);
}

void mainWin::eventListener( event* ev ){
    window::eventListener(ev);
}
